package lol.oxguy3.android.mcmc;

/**
 * Represents a Minecraft server in the list in ServerListActivity
 * 
 */
public class MinecraftServer {
	String name;
	String address; //TODO IPv6 support
	int port;
	
	String motd; //TODO Color code support
	int playersOnline;
	int playersMax;
	int ping;
	
	/**
	 * Shortcut constructor for MinecraftServer(n, addr, 25565)
	 * 
	 * @param n the user-set server name
	 * @param a the server address (without the port number)
	 */
	public MinecraftServer(String n, String a) {
		this(n, a, 25565);
	}
	
	/**
	 * Creates new MinecraftServer instance and sends the server a Server List Ping packet to fill in the other variables
	 * 
	 * @param n the user-set server name
	 * @param a the server address (without the port number)
	 * @param p the server's port number
	 */
	public MinecraftServer(String n, String a, int p) {
		name = n;
		address = a; //TODO Check if address is valid
		port = p;
		if (p < 0 || p > 65535) {
			port = 25565;
		}
		//TODO update and set motd, playersOnline, playersMax, pingtime
	}
	
	
	
	//TODO implement this!!!!!
	/**
	 * Method to poll the server for server-side information and store the info in this object
	 * Based heavily on net.minecraft.src.ServerNBTStorage.pollServer(ServerNBTStorage)
	 * 
	 * @throws IOException
	 */
    /*private void pollServer() throws IOException
    {
        String s = getDisplayAddress();
        String as[] = s.split(":");

        if (s.startsWith("["))
        {
            int i = s.indexOf("]");

            if (i > 0)
            {
                String s2 = s.substring(1, i);
                String s3 = s.substring(i + 1).trim();

                if (s3.startsWith(":") && s3.length() > 0)
                {
                    s3 = s3.substring(1);
                    as = new String[2];
                    as[0] = s2;
                    as[1] = s3;
                }
                else
                {
                    as = new String[1];
                    as[0] = s2;
                }
            }
        }

        if (as.length > 2)
        {
            as = new String[1];
            as[0] = s;
        }

        String s1 = as[0];
        int j = as.length <= 1 ? 25565 : parseIntWithDefault(as[1], 25565);
        Socket socket = null;
        DataInputStream datainputstream = null;
        DataOutputStream dataoutputstream = null;

        try
        {
            socket = new Socket();
            socket.setSoTimeout(3000);
            socket.setTcpNoDelay(true);
            socket.setTrafficClass(18);
            socket.connect(new InetSocketAddress(s1, j), 3000);
            datainputstream = new DataInputStream(socket.getInputStream());
            dataoutputstream = new DataOutputStream(socket.getOutputStream());
            dataoutputstream.write(254);

            if (datainputstream.read() != 255)
            {
                throw new IOException("Bad message");
            }

            String s4 = Packet.readString(datainputstream, 256);
            char ac[] = s4.toCharArray();

            for (int k = 0; k < ac.length; k++)
            {
                if (ac[k] != '\247' && ChatAllowedCharacters.allowedCharacters.indexOf(ac[k]) < 0)
                {
                    ac[k] = '?';
                }
            }

            s4 = new String(ac);
            String as1[] = s4.split("\247");
            s4 = as1[0];
            int l = -1;
            int i1 = -1;

            try
            {
                l = Integer.parseInt(as1[1]);
                i1 = Integer.parseInt(as1[2]);
            }
            catch (Exception exception) { }

            par1ServerNBTStorage.motd = (new StringBuilder()).append("\2477").append(s4).toString();

            if (l >= 0 && i1 > 0)
            {
                par1ServerNBTStorage.playerCount = (new StringBuilder()).append("\2477").append(l).append("\2478/\2477").append(i1).toString();
            }
            else
            {
                par1ServerNBTStorage.playerCount = "\2478???";
            }
        }
        finally
        {
            try
            {
                if (datainputstream != null)
                {
                    datainputstream.close();
                }
            }
            catch (Throwable throwable) { }

            try
            {
                if (dataoutputstream != null)
                {
                    dataoutputstream.close();
                }
            }
            catch (Throwable throwable1) { }

            try
            {
                if (socket != null)
                {
                    socket.close();
                }
            }
            catch (Throwable throwable2) { }
        }
    }*/
	
	
	
	
	/**
	 * Returns the user-set server name
	 * 
	 * @return the name
	 */
	public String getName() {
		return name;
	}
	
	/**
	 * Returns the server's address (not including port)
	 * 
	 * @return the address
	 */
	public String getAddress() {
		return address;
	}
	
	/**
	 * Returns the server port number
	 * 
	 * @return the port
	 */
	public int getPort() {
		return port;
	}
	
	/**
	 * Returns the MOTD set by the server
	 * 
	 * @return the MOTD, or null if can't server is unreachable
	 */
	public String getMotd() {
		return motd;
	}
	
	/**
	 * Returns the number of players currently on the server
	 * 
	 * @return the player count, or null if server is unreachable
	 */
	public int getPlayersOnline() {
		return playersOnline;
	}
	
	/**
	 * Returns the maximum number of players the server says it can handle
	 * 
	 * @return the max player count, or null if server is unreachable
	 */
	public int getPlayersMax() {
		return playersMax;
	}
	
	/**
	 * Returns the server ping, or the number of milliseconds it took the server to reply to the last request
	 * 
	 * @return the ping time, or null if server is unreachable
	 */
	public int getPing() {
		return ping;
	}
	
	
	/**
	 * Returns a string containing the address and the port
	 * 
	 * @return the server IP in the format "<ADDRESS>:<PORT>"
	 */
	public String getDisplayAddress() {
		return address + ":" + String.valueOf(port);
	}
	
	
	
	/**
	 * Sets the user-set server name
	 * 
	 * @param n the new name
	 */
	public void setName(String n) {
		name = n;
	}
	
	/**
	 * Sets the server's address (not including port)
	 * 
	 * @param n the new address without a port
	 * @returns true if the address is valid (NOT YET IMPLEMENTED)
	 */
	public boolean setAddress(String a) {
		address = a; //TODO Check if address is valid
		return true;
	}
	
	/**
	 * Sets the server's port number
	 * 
	 * @param n the new port
	 * @returns true if the port number was valid (valid ports are 0-65535 inclusively)
	 */
	public boolean setPort(int p) {
		if (p < 0 || p > 65535) {
			return false;
		}
		port = p;
		return true;
	}
	
	
	/**
	 * Override to make the ArrayAdapter that populates ServerListActivity show server names instead of a default toString() output
	 * 
	 */
	@Override
	public String toString() {
		return name;
		/*return getClass().getName() + "[" +
				"name=" + name + ", " +
				"address=" + address + ", " +
				"port=" + Integer.toString(port) + "]";*/
	}

}
